package cn.edu.hhu.teacher;

import cn.edu.hhu.Bean.AttendanceTask;
import cn.edu.hhu.teacher.bean.Course;
import cn.edu.hhu.teacher.bean.Teacher;
import cn.edu.hhu.teacher.service.CourseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet("/teacher/addtask")
public class AddTaskServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CourseService courseService = new CourseService();
        Course course = courseService.get((Teacher) request.getSession().getAttribute("teacher"), request.getParameter("cid"));
        if (course == null) {
            response.sendRedirect(request.getContextPath() + "/teacher/index");
            return;
        }
        String tname = request.getParameter("tname");
        String pwd = request.getParameter("pwd");
        String start = request.getParameter("start_time");
        String end = request.getParameter("end_time");
        if (tname == null || pwd == null || start == null || end == null) {
            request.setAttribute("msg", "添加失败！");
            doGet(request, response);
            return;
        }
        try {
            Timestamp start_time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").parse(start).getTime());
            Timestamp end_time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").parse(end).getTime());
            if (start_time.compareTo(end_time) >= 0) {
                request.setAttribute("msg", "开始时间晚于结束时间！");
                doGet(request, response);
                return;
            }
            AttendanceTask task = new AttendanceTask(0, course.getCid(), start_time, end_time, pwd, new Timestamp(System.currentTimeMillis()), tname);
            if (courseService.addTask(task)) {
                request.setAttribute("msg", "任务添加成功！<br>请返回签到任务管理页面查看或继续添加。");
                doGet(request, response);
            } else {
                request.setAttribute("msg", "添加失败！");
                doGet(request, response);
            }
        } catch (ParseException e) {
            request.setAttribute("msg", "添加失败！");
            doGet(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CourseService courseService = new CourseService();
        Course course = courseService.get((Teacher) request.getSession().getAttribute("teacher"), request.getParameter("cid"));
        if (course == null) {
            response.sendRedirect(request.getContextPath() + "/teacher/index");
            return;
        }
        request.setAttribute("course", course);
        request.setAttribute("now", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(System.currentTimeMillis()));
        request.getRequestDispatcher("/WEB-INF/view/teacher/addtask.jsp").forward(request, response);
    }
}
